﻿//-----------------------------------------------------------------------
// <copyright file="CategoryModel.cs" company="Microsoft Corporation">
//     Copyright (c) Alm Rangers, Microsoft Corporation. All rights reserved.
// </copyright>
//-----------------------------------------------------------------------
namespace Microsoft.ALMRangers.VsarTreasureMap.WindowsStoreApp.ViewModels
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using Microsoft.ALMRangers.VsarTreasureMap.WindowsStoreApp.Models;
    using Microsoft.ALMRangers.VsarTreasureMap.WindowsStoreApp.MVVMSupport;
    using Windows.ApplicationModel.DataTransfer;

    /// <summary>
    /// Object representation of a CategoryModel
    /// </summary>
    public class Category : IShare
    {
        /// <summary>
        /// Initializes a new instance of the CategoryModel class.  This class is the view model
        /// for the CategoryModel page.
        /// </summary>
        /// <param name="id">Unique identifier for this category.</param>
        public Category(int id)
        {
            this.GoBack = new Command(() =>
            {
                Navigation.GoBack();
            });

            this.GoToHome = new Command(() =>
            {
                Navigation.Navigate(typeof(Views.TreasureMapPage));
            });

            var category = App.Database.Categories.Single(_ => _.Id == id);
            this.CategoryTitle = category.Name;
            this.CategoryImage = category.Image;
            this.CategoryDescription = category.Description;
            this.Projects = new List<Guidance>(category.Projects.Where(_ => _.Category == id).SelectMany(_ => _.Guidance));
            DataTransferManager.GetForCurrentView().DataRequested += this.Category_DataRequested;
        }

        /// <summary>
        /// Gets the CategoryModel's Title
        /// </summary>
        public string CategoryTitle { get; private set; }

        /// <summary>
        /// Gets or sets the CategoryModel's image uri
        /// </summary>
        public string CategoryImage { get; set; }

        /// <summary>
        /// Gets or sets the CategoryModel's full description
        /// </summary>
        public string CategoryDescription { get; set; }

        /// <summary>
        /// Gets the command for the back button
        /// </summary>
        public Command GoBack { get; private set; }

        /// <summary>
        /// Gets the command for the home button
        /// </summary>
        public Command GoToHome { get; private set; }

        /// <summary>
        /// Gets the List of Guidance of which are projects.
        /// </summary>
        public List<Guidance> Projects { get; private set; }

        /// <summary>
        /// Gets or sets the selected property
        /// What this really does is returns null to a property bound to it
        /// however when this property is set (on an item selected event, same as an item clicked event)
        /// the set is called, which makes a call to navigate.
        /// This is used as a slick alternative to creating an attached dependency property of type ICommand
        /// as it uses the an unused property.  Binding null does not throw an exception.
        /// </summary>
        public Guidance Selected
        {
            get
            {
                return null;
            }

            set
            {
                var index = this.Projects.IndexOf(value);
                var parameter = Tuple.Create(this.Projects, index);
                Navigation.Navigate(typeof(Microsoft.ALMRangers.VsarTreasureMap.WindowsStoreApp.Views.Item), parameter);
            }
        }

        /// <summary>
        /// Deregisters charm events as to not have multiple instances of the same handler for the same raised event.
        /// </summary>
        public void Deregister()
        {
            DataTransferManager.GetForCurrentView().DataRequested -= this.Category_DataRequested;
        }

        /// <summary>
        /// Sharing event handler that is called when a user attempts to share from this page.
        /// </summary>
        /// <param name="sender">The parameter is not used.</param>
        /// <param name="args">The parameter is not used.</param>
        public void Category_DataRequested(DataTransferManager sender, DataRequestedEventArgs args)
        {
            args.Request.FailWithDisplayText("Nothing can be shared on this page, but other pages do have treasure that can be shared! Happy exploring!");
        }
    }
}